package com.loadmore.ibook;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.loadmore.common.api.Api;
import com.loadmore.common.bean.Book;
import com.loadmore.common.bean.Response;
import com.loadmore.common.constants.BookConstants;
import com.loadmore.common.constants.Constants;
import com.loadmore.common.net.RequestUtil;
import com.loadmore.common.net.impl.RequestCallBack;
import com.loadmore.common.util.JsonUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ISBNActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView book_info;
    private Book book;
    private LinearLayout edit_query;
    private EditText title;
    private Button update;
    private Button delete;
    private boolean edit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_isbn);
        book_info = findViewById(R.id.book_info);
        edit_query = findViewById(R.id.edit_query);
        title = findViewById(R.id.title);
        update = findViewById(R.id.update);
        delete = findViewById(R.id.delete);
        Bundle bundle = getIntent().getExtras();
        edit = bundle.getBoolean(BookConstants.EDIT);
        book = (Book) bundle.getSerializable(Constants.DATA);
        book_info.setText(book.getCatalog());
        update.setOnClickListener(this);
        delete.setOnClickListener(this);
        if (edit) {
            edit_query.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.update:
                String titleStr = title.getText().toString();
                if (!TextUtils.isEmpty(titleStr)) {
                    book.setTitle(titleStr);
                    updateBook(book);
                }
                break;
            case R.id.delete:
                deleteBook(book);
                break;
        }
    }

    private void updateBook(Book book) {

        if (book == null)
            return;
        Map<String, String> params = new HashMap<>();
        params.put("isbn", book.getIsbn());
        params.put("title", book.getTitle());
        params.put("image", book.getImage());
        params.put("catalog", book.getCatalog());
        params.put("author", book.getAuthor());
        params.put("price", book.getPrice());
        params.put("subtitle", book.getSubtitle());
        params.put("publisher", book.getPublisher());
        params.put("summary", book.getSummary());
        RequestUtil.getInstance(this).post(Api.insert_isbn, params, new RequestCallBack() {
            @Override
            public void onSuccess(String data) {

                Response response = JsonUtil.getJson(data, Response.class);
                if (response != null && response.getCode() == 200 && !TextUtils.isEmpty(response.getData())) {
                    Toast.makeText(ISBNActivity.this, "update success", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(ISBNActivity.this, "update failed", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(String error) {

            }
        });
    }


    private void deleteBook(Book book) {
        Map<String, String> params = new HashMap<>();
        params.put("isbn", book.getIsbn());
        RequestUtil.getInstance(this).post(Api.delete_isbn, params, new RequestCallBack() {
            @Override
            public void onSuccess(String data) {
                Response response = JsonUtil.getJson(data, Response.class);
                if (response != null && response.getCode() == 200) {
                    finish();
                }

            }

            @Override
            public void onError(String error) {

            }
        });
    }
}
